package com.atguigu.staservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.javafx.collections.MappingChange;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-28
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {
    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {

        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        baseMapper.delete(wrapper);

        //远程调用某一天注册人数
        R registerR = ucenterClient.countRegisterDay(day);
        Integer registerCount = (Integer) registerR.getData().get("countRegisterDay");

        //把获取到的数据添加到数据库统计分析表中
        StatisticsDaily sta = new StatisticsDaily();

        sta.setRegisterNum(registerCount);//人数
        sta.setDateCalculated(day);//统计日期

        sta.setLoginNum(RandomUtils.nextInt(100, 200));
        sta.setVideoViewNum(RandomUtils.nextInt(100, 200));
        sta.setCourseNum(RandomUtils.nextInt(100, 200));
        baseMapper.insert(sta);
    }
    //图表显示，返回两部分数据，日期json数据，数量json数据

    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {

        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);

        List<StatisticsDaily> dailyList = baseMapper.selectList(wrapper);

        List<String> dataList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();

        for (int i = 0; i < dailyList.size(); i++) {
            StatisticsDaily sta = dailyList.get(i);
            //封装日期数据
            dataList.add(sta.getDateCalculated());

            switch (type) {
                case "login_num":
                    countList.add(sta.getLoginNum());
                    break;
                case "register_num":
                    countList.add(sta.getRegisterNum());
                    break;
                case "video_view_num":
                    countList.add(sta.getVideoViewNum());
                    break;
                case "course_num":
                    countList.add(sta.getCourseNum());
                    break;
                default:
                    break;

            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("dataList", dataList);
        map.put("countList", countList);
        return map;
    }
}
